package icu.wangrui.bookstore.controller;

import icu.wangrui.bookstore.dto.BookDTO;
import icu.wangrui.bookstore.pojo.Book;
import icu.wangrui.bookstore.service.impl.BookServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.util.List;

@Controller
public class AdminController {
    @Autowired
    private BookServiceImpl bookService;
    @GetMapping("/admin/login")
    public String adminLogin(@PathParam("username")String username,
                             @PathParam("password")String password,
                             @PathParam("rememberMe")String rememberMe,
                             Model model,
                             HttpSession session){
        //具体业务
        if(!StringUtils.isEmpty(username)&&"1".equals(password)){
            session.setAttribute("loginAdmin",username);
            //登录成功跳转后台主页
            return "redirect:/adminindex.html";
        }else {
            //告诉用户登录失败
            model.addAttribute("msg","用户名或密码错误");
            return "/admin/page/login";
        }
    }
    @GetMapping("/admin/logout")
    public String logout(HttpSession session){
        session.removeAttribute("loginAdmin");
        /*
         * session.invalidate()是将session设置为失效，一般在退出时使用，
         * 但要注意的是：session失效的同时 浏览器会立即创建一个新的session的，
         * 你第一个session已经失效了 所以调用它的getAttribute方法时候一定会抛出NullPointerException的
         * session.invalidate();
         * */
        return "redirect:/adminlogin.html";
    }


    @GetMapping("/admin/product_list")
    public String product_list(Model model){
        List<Book> books = bookService.adminShowAllBook();
        model.addAttribute("books",books);
        return "/admin/page/product-list";
    }

    @GetMapping("/admin/product_detail")
    public String product_detail(Model model,
                                 @RequestParam(name = "isbn") String isbn){
        Book book = bookService.showBookByIsbn(isbn);
        model.addAttribute("book",book);
        return "/admin/page/product-detail";
    }

    @GetMapping("/admin/product_edit")
    public String product_edit(Model model,
                                 @RequestParam(name = "isbn") String isbn){
        Book book = bookService.showBookByIsbn(isbn);
        model.addAttribute("book",book);
        return "/admin/page/product-edit";
    }

    @GetMapping("/admin/product_update")
    public String product_update(
                               @RequestParam(name = "price") Double price,
                               @RequestParam(name = "isbn") String isbn){
        bookService.updateBookByIsbn(isbn,price);
        return "redirect:/admin/product_list";
    }

    @GetMapping("/admin/product_add")
    public String product_add(){
        return "/admin/page/product-add";
    }

    @RequestMapping("/admin/product_addTo")
    @ResponseBody
    public Boolean product_addTo(Book book){
        Boolean aBoolean = bookService.addBook(book);
        return aBoolean;
    }

    @RequestMapping("/admin/searchBookByIsbn")
    @ResponseBody
    public Boolean searchBookByIsbn(
                                 @RequestParam(name = "isbn") String isbn){
        Book book = bookService.showBookByIsbn(isbn);
        if (book!=null){
            return true;
        }else {
            return false;
        }
    }
}
